
Android开发文档翻译:安装Google Play Services SDK,Set Up Google Play Services SDK
要想使用Google Play services APIs来开发一个程序,妳必须用 SDK Manager 来下载Google Play services SDK。下载的内容中会包含客户端库及示例代码。
为了在妳的程序中测试Google Play services SDK,妳必须使用以下两个东西中的一个:
•.一个与Android 兼容的运行着Android 2.3 或更高版本并且装有Google Play Store 的设备。
•.Android 模拟器中的一个运行着基于Android 4.2.2 或更高版本的Google APIs 平台的AVD 。
理想情况下,妳应当在包括手机和平板在内的多种设备上开发及测试妳的程序。
按照以下步骤来安装上开发用的Google Play services SDK:
1.使用以下多种方式中的一种来启动SDK Manager:
•.在Eclipse中 (已装有ADT),单击 Window > Android SDK Manager 。
•.在Mac或Linux上,打开一个终端,切换到Android SDK 中的 tools/ 目录,然后执行 android sdk 。
2.安装Google Play services SDK。
滚动到软件包列表的底部,展开Extras ,选中 Google Play services ,然后安装。
Google Play services SDK会被保存到妳的Android SDK 环境中的<android-sdk>/extras/google/google_play_services/ 。
注意 : Google Play services 4.0.30 (于2013年11月发布)及更高的版本都只支持Android 2.3 及更高的版本。如果妳的程序支持Android 2.2,那么,妳仍然可以使用Google Play services SDK 来进行开发,但是必须在SDK Manager 中安装 Google Play services for Froyo 。
3.安装一个相兼容的Google APIs 平台版本。
如果妳想要在模拟器中测试妳的程序,展开 Android 4.2.2 (API 17) 或更高版本的目录,选中 Google APIs ,然后安装。然后创建一个以 Google APIs 作为平台目标的 AVD 。
4.将Google Play services 库项目复制一份。
将位于<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/的库项目复制到妳自己放置Android 程序项目的地方。
如果妳用的是Eclipse,则将这个库项目导入到工作空间里。点击 File > Import ,选择 Android > Existing Android Code into Workspace ,然后浏览到妳所复制的那个库项目目录,导入它。
使用Eclipse或其它的IDE:
为了在妳的程序中使用Google Play services 接口(APIs),妳必须引用自己在 安装说明 里第4步创建的库项目。
参考在 Eclipse 中引用一个库项目或在命令行引用一个库项目以了解该如何完成这个操作。
注意 : 妳应当引用自己复制到开发工作空间里去的那一份库——不应当直接从Android SDK 目录里引用那个库。
当妳在项目中引用好了这个库项目之后,就可以开始利用Google Play services接口来开发了。
在<project_directory>/proguard-project.txt文件中加入以下代码,以阻止 ProGuard 将必要的类裁剪掉:
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@ccom.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
在Google Play services介绍中有说明,Google Play通过Google Play Store 程序来向Android 2.2 的设备上推送服务更新。然而,更新信息可能不会立即到达每个用户。
重要事项 : 由于难以预知每个设备的状态,所以,妳必须 每次都检查当前设备上是否存在一个兼容的Google Play services APK,然后才能使用那些与Google Play services 相关的功能。对于狠多程序来说,做这个检查的最佳时机就是在主活动对象(main activity)的 onResume() 方法里。
以下是在用户的设备上Google Play services APK 的4种可能状态:
1.安装了较新版本的Google Play Store 程序,并且已经下载最新版本的Google Play services APK。
2.安装了较新版本的Google Play Store 程序,但 没有 下载最新版本的Google Play services APK。
3.当前有一个较旧版本的Google Play Store 程序,它并没有主动地去下载Google Play services 更新包。
4.在当前设备上,找不到Google Play services APK,或者已经被禁用,这可能是因为用户明确地卸载或禁用了它。
第1种情况就是正确的状态,也是最常见的状态。然而,其它情况也可能会发生,所以,妳必须在每次连接到Google Play service 的时候检测一下Google Play services APK 是否存在,是否是最新版,是否被启用了。
我们已经帮妳实现了这一点,Google Play services客户端库中有些辅助方法,可用于确定:当前设备上是否存在着版本足够新的满足妳的需要的Google Play services APK。如果不存在,则,客户端库会带用户跳转到Google Play Store,下载最新版本的Google Play services APK。
注意 :在Google Play Store 中搜索时是找不到Google Play services APK 的。当客户端库检测到设备上没有Google Play services APK 或者其版本不兼容时,会直接链接到Google Play Store 中狠深的一处链接来下载。
由妳来自行决定在程序中的什么地方做以下检查Google Play services APK 的步骤。例如,如果Google Play services是妳的程序的必需功能的话,那么,妳可能需要在程序启动的时候就做这个检查了。另一方面,如果Google Play services只是妳程序中可选的一个功能,那么,妳可以在用户切换到妳程序中对应的页面时再做这种检查:
1.使用 isGooglePlayServicesAvailable() 方法来查询设备上Google Play services 的状态,这个方法会返回一个状态码。
2.如果结果的状态码为 SUCCESS ,那么Google Play services APK已经是最新版了,妳可以让程序按照正常流程来工作。
3.如果结果状态码是 SERVICE_MISSING 、 SERVICE_VERSION_UPDATE_REQUIRED 或 SERVICE_DISABLED ,那么,调用 getErrorDialog() ,它会显示一条错误信息,带领用户从 Google Play Store 下载这个APK 或者要求用户在设备的系统设置里启用它。
未知美人
HxLauncher: Launch Android applications by voice commands